import { createRouter, createWebHistory } from "vue-router";
import Home from "../views/Home.vue";

//pinia
import { useUserStore } from '@/store/user'

const routes = [
    {
        path: "/",
        name: "Home",
        component: Home,
    },
    {
        path: "/about",
        name: "About",
        beforeEnter: (to, from, next) => {
            if (true) {
                next()
            } else {
                next('/login')
            }
        },
        component: () =>
            import(/* webpackChunkName: 'about' */ "../views/About.vue"),
    },
    {
        path: "/course",
        name: "Course",
        component: () =>
            import(/* webpackChunkName: 'course' */ "@/views/Course.vue"),

    },
    {
        path: "/course/detail",
        name: "Detail",
        component: () =>
            import(/* webpackChunkName: "detail" */ "@/views/Detail.vue"),
    },
    { // 课程播放详情页
        path: '/course-play/:courseId/:chapterId',
        name: 'course-play',
        component: () =>
            import(/* webpackChunkName: "CoursePlay" */ "../views/CoursePlay.vue"),
    },
    {
        path: "/login",
        name: "Login",
        component: () =>
            import(/* webpackChunkName: "Login" */ "@/views/Login.vue"),
    },
    {
        path: "/cart",
        name: "Cart",
        component: () =>
            import(/* webpackChunkName: "cart" */ "@/views/Cart.vue"),
        beforeEnter: (to, from, next) => {
            console.log(useUserStore().userInfo.id)
            // 如果有用户信息id
            if (useUserStore().userInfo.id) {
                next();
            } else {
                next('/login');
            }
        }
    },
    { // 确定订单页
        path: "/confirmOrder",
        name: "ConfirmOrder",
        component: () =>
            import(/* webpackChunkName: "confirmOrder" */ "@/views/ConfirmOrder.vue"),
        beforeEnter: (to, from, next) => {

            // 如果有用户信息id
            if (useUserStore().userInfo.id) {
                next();
            } else {
                next('/login');
            }
        }
    },


];

const router = createRouter({
    history: createWebHistory(),
    routes,
});

export default router;
